/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      FileDocumentStorage.hpp
 *
 * $FTS$
 */
#ifndef _FILE_DOCUMENT_STORAGE_HPP__
#define _FILE_DOCUMENT_STORAGE_HPP__ 1

/**
  @file DocumentStorage.hpp
  @brief Document storage
*/
#include <DocumentStorage.hpp>
#include <DataDumper.hpp>

namespace FTSS // FullTextSearchSystem
{

#define C_READ_MODE       0x00000001
#define C_WRITE_MODE      0x00000002
#define C_READ_WRITE_MODE 0x00000003

#define C_DOC_STORAGE_DATA_READABLE  "Parsed documents storage (data file)"
#define C_DOC_STORAGE_DATA_TYPE      0x00000004

#define C_DOC_STORAGE_IDX_READABLE   "Parsed documents storage (index file)"
#define C_DOC_STORAGE_IDX_TYPE       0x00000005

#define C_IDX_RECNUM_OFFSET              (sizeof(UINT_32) + sizeof(UINT_32) + C_FTSS_READABLE_LENGTH)
#define C_IDX_DATA_OFFSET                (sizeof(UINT_32) + sizeof(UINT_32) + C_FTSS_READABLE_LENGTH + sizeof(UINT_32))

/**
  @class DocumentStorage DocumentStorage.hpp <DocumentStorage.hpp>
  @brief Document data base storage
*/
class FileDocumentStorage:
  public DocumentStorage
{
public:
	FileDocumentStorage(CCHAR_P szIndexFile, CCHAR_P szDataFile, const UINT_32 & iMode);

	/**
	  @brief A Destructor
	*/
	~FileDocumentStorage() throw();

	/**
	  @brief Flush buffers to the storage
	*/
	void Sync();

private:
	/** Index file handle    */
	INT_32     iIndexHandle;
	/** Data file handle     */
	INT_32     iDataHandle;
	/** Number of documents  */
	INT_32     iRecords;
	/** Data file size       */
	UINT_64    iDataSize;


	UINT_32    mmapFlags;
public : 
	/**
	  @brief Get document by specified ID
	  @return Pointer to the document or NULL if document does not exist
	*/
	Document * GetDocument(const DocumentId_t & iDocumentId);

	/**
	  @brief Store document information
	  @param pDocument - pointer to the document
	  @return Storage document id
	*/
	DocumentId_t StoreDocument(const Document * pDocument);

	/**
	  @brief Store document as set of his parts
	  @param aParts - parts of document
	  @param iPartsSize - size of parts
	  @param aIndex - index with term offsets
	  @param iIndexSize - size of index
	  @param iTextSize - size of text data
	  @param sText - text data
	  @param iTextSize - size of text data
	  @return Storage document id
	*/
	DocumentId_t StoreDocumentParts(const DocumentPart   * aParts,
	                                const UINT_32        & iPartsSize,
	                                const DocumentIndex  * aIndex,
	                                const UINT_32        & iIndexSize,
	                                const UCCHAR_P         sText,
	                                const UINT_32        & iTextSize);

	// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private:
	/**
	  @brief Check existence of a non-empty file
	  @param szFile - file to check
	  @return 0 if file exists, -1 if not
	*/
	INT_32 NonEmptyFileExists(CCHAR_P szFile);

	/**
	  @brief Open storage file
	  @param szFile - file to open
	  @param iMode - file mode
	  @param iIndexFile - if index or data file
	*/
	void OpenStorage(CCHAR_P szFile, const UINT_32 & iMode, const INT_32 & iIndexFile);
};

} // namespace FTSS
#endif // _DOCUMENT_STORAGE_HPP__
// End.
